import mongoose from "mongoose";
import {config} from "./config/config";
import logger from "./logger";

const connectDB = async (): Promise<void> => {
    try {
        const url: string = `mongodb://${config.db.root}@${config.db.host}:${config.db.port}/${config.db.name}`;
        const options: mongoose.ConnectOptions = {
            maxPoolSize: 10, // 设置连接池最大大小为10
        };
        await mongoose.connect(url, options);
    } catch (err) {
        logger.error("Database connection error: ", err);
        process.exit(1);
    }

    mongoose.connection.on('error', (err) => {
        logger.error({msg: 'db err', err: err.stack});
    });
};

export default connectDB;
